package com.hu.gulimallproduct.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.gulimallcommon.utils.PageUtils;

import com.hu.gulimallcommon.utils.Query;
import com.hu.gulimallproduct.project.dao.PmsAttrAttrgroupRelationDao;
import com.hu.gulimallproduct.project.dao.PmsAttrGroupDao;
import com.hu.gulimallproduct.project.entity.PmsAttrAttrgroupRelationEntity;
import com.hu.gulimallproduct.project.entity.PmsAttrEntity;
import com.hu.gulimallproduct.project.entity.PmsAttrGroupEntity;
import com.hu.gulimallproduct.project.service.PmsAttrGroupService;
import com.hu.gulimallproduct.project.service.PmsAttrService;
import com.hu.gulimallproduct.project.vo.AttrGroupCatelogIdVO;
import com.hu.gulimallproduct.project.vo.AttrGroupRelationVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service()
public class PmsAttrGroupServiceImpl extends ServiceImpl<PmsAttrGroupDao, PmsAttrGroupEntity> implements PmsAttrGroupService {
    @Autowired
    PmsAttrAttrgroupRelationDao attrgroupRelationDao;

    @Autowired
    PmsAttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        IPage<PmsAttrGroupEntity> page ;
        QueryWrapper<PmsAttrGroupEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)) {
            queryWrapper.and((obj)->{
                obj.eq("attr_group_id",key)
                        .or()
                        .like("attr_group_name",key);
            });
        }
        if(catelogId==0L){
            page= this.page(
                    new Query<PmsAttrGroupEntity>().getPage(params),queryWrapper

            );
        }else {
            page = this.page(
                    new Query<PmsAttrGroupEntity>().getPage(params),
                    queryWrapper
                            .eq("catelog_id",catelogId));

        }
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsAttrGroupEntity> page = this.page(
                new Query<PmsAttrGroupEntity>().getPage(params),
                new QueryWrapper<PmsAttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void deleteRelation(AttrGroupRelationVO[] groupRelationVO) {
        List<PmsAttrAttrgroupRelationEntity> collect = Arrays.asList(groupRelationVO).stream().map((x) -> {
            PmsAttrAttrgroupRelationEntity entity = new PmsAttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(x, entity);
            return entity;
        }).collect(Collectors.toList());
        attrgroupRelationDao.deleteBeachGroupRelation(collect);
    }

    @Override
    public List<AttrGroupCatelogIdVO> withattrByCategoryId(Long catelogId) {
        List<PmsAttrGroupEntity> groupEntities = this.list(new QueryWrapper<PmsAttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupCatelogIdVO> collect = groupEntities.stream().map((item) -> {
            AttrGroupCatelogIdVO catelogIdVO = new AttrGroupCatelogIdVO();
            BeanUtils.copyProperties(item, catelogIdVO);
            List<PmsAttrEntity> pmsAttrEntities = attrService.listRelationAttr(item.getAttrGroupId());
            catelogIdVO.setAttrs(pmsAttrEntities);
            return catelogIdVO;
        }).collect(Collectors.toList());
        return collect;
    }
}